<script>
import {getCurrentInstance, provide, ref} from 'vue'
import {common} from '../../api/common'
import {postRequest} from '../../api/star_horse'

export default {
  name: 'Demo',

  mixins: [common],
  setup() {
    const searchForm = ref({})
    const ruleForm = ref({})
    const {proxy} = getCurrentInstance()
    const commonFunction = function (funcName, params) {
      let funs = proxy[funcName]
      funs.call(proxy, params)
    }
    provide('commonFunction', commonFunction)
    provide('searchForm', searchForm)
    provide('dataForm', ruleForm)
    return {searchForm, ruleForm}
  },
  data() {
    const unitList = ref([])
    const loadByPageUrl = '/doov-lean-materials/materials/generalMaterialsSummary/pageList'
    const mergeUrl = '/doov-lean-materials/materials/generalMaterialsSummary/merge'
    const mergeDraftUrl = '/doov-lean-materials/materials/generalMaterialsSummary/mergeDraft'
    const batchMergeUrl = '/doov-lean-materials/materials/generalMaterialsSummary/mergeBatch'
    const batchMergeDraftUrl = '/doov-lean-materials/materials/generalMaterialsSummary/mergeBatchDraft'
    const loadByIdUrl = '/doov-lean-materials/materials/generalMaterialsSummary/getById'
    const deleteUrl = '/doov-lean-materials/materials/generalMaterialsSummary/batchDeleteById'
    const exportAllUrl = '/doov-lean-materials/materials/generalMaterialsSummary/exportData'
    const downloadTemplateUrl = '/doov-lean-materials/materials/generalMaterialsSummary/downloadTemplate'
    const uploadUrl = '/doov-lean-materials/materials/generalMaterialsSummary/importData'
    const userConditionUrl = '/doov-lean-materials/materials/generalMaterialsSummary/getAllByCondition'
    const materialsOneConditionUrl = '/doov-lean-materials/materials/incomeMaterials/getOneByCondition'
    const primaryKey = 'idGeneralMaterialsSummary'
    return {
      loadByPageUrl,
      mergeUrl,
      mergeDraftUrl,
      batchMergeUrl,
      batchMergeDraftUrl,
      loadByIdUrl,
      deleteUrl,
      exportAllUrl,
      downloadTemplateUrl,
      uploadUrl,
      userConditionUrl,
      unitList,
      materialsOneConditionUrl,
      tableDataId: 'generalMaterialsSummary',
      primaryKey: primaryKey || 'unknown',
      searchFormData: [
        /*     {label: "主键", fieldName: "idGeneralMaterialsSummary", type: "long"}, */
        {label: '名称', fieldName: 'name', type: 'string'}
        /*    {label: "描述", fieldName: "bomDesc", type: "string"}, */
      ],
      tableFieldList: {
        fieldList: [
          {
            label: '主键',
            fieldName: 'idGeneralMaterialsSummary',
            type: 'long',
            required: false,
            formShow: false,
            tableShow: false,
            minWidth: 180
          },
          {
            label: '名称',
            fieldName: 'name',
            type: 'string',
            required: true,
            formShow: !false,
            tableShow: !false,
            minWidth: 180
          },
          {
            label: '描述',
            fieldName: 'bomDesc',
            type: 'textarea',
            required: false,
            formShow: !false,
            tableShow: !false,
            minWidth: 180
          },
          {
            label: '修改人',
            fieldName: 'updatedBy',
            type: 'string',
            required: false,
            formShow: !true,
            tableShow: true,
            minWidth: 180
          },
          {
            label: '修改时间',
            fieldName: 'updatedTime',
            type: 'date',
            required: false,
            formShow: !true,
            tableShow: true,
            minWidth: 180
          },
          {
            label: '创建人',
            fieldName: 'createdBy',
            type: 'string',
            required: false,
            formShow: !true,
            tableShow: true,
            minWidth: 180
          },
          {
            label: '创建时间',
            fieldName: 'createdTime',
            type: 'date',
            required: false,
            formShow: !true,
            tableShow: true,
            minWidth: 180
          },
          {
            label: '数据编号',
            fieldName: 'dataNo',
            type: 'string',
            required: false,
            formShow: !true,
            tableShow: !true,
            minWidth: 180
          },
          {
            label: '是否删除 1是 0 否 默认0',
            fieldName: 'isDel',
            type: 'integer',
            required: false,
            formShow: !true,
            tableShow: !true,
            minWidth: 180
          },
          {
            label: '版本 默认1',
            fieldName: 'version',
            type: 'integer',
            required: false,
            formShow: !true,
            tableShow: !true,
            minWidth: 180
          },
          {
            label: '数据状态 ',
            fieldName: 'statusName',
            type: 'string',
            required: false,
            formShow: !true,
            tableShow: !true,
            minWidth: 180
          },
          {
            label: '状态编码',
            fieldName: 'statusCode',
            type: 'string',
            required: false,
            formShow: !true,
            tableShow: !true,
            minWidth: 180
          }
        ],
        batchFieldList: [
          {
            label: '主键',
            fieldName: 'idGeneralMaterialsDetails',
            type: 'long',
            required: false,
            formShow: false,
            tableShow: !false,
            minWidth: 180
          },
          {
            label: '主键',
            fieldName: 'idGeneralMaterialsSummary',
            type: 'long',
            required: false,
            formShow: false,
            tableShow: !false,
            minWidth: 180
          },

          {
            label: 'Bom组件',
            fieldName: 'bomNo',
            type: 'string',
            required: true,
            formShow: !false,
            tableShow: !false,
            minWidth: 180,
            func: {
              type: 'input',
              name: 'loadBomDataInfo'
            }
          },
          {
            label: '名称',
            fieldName: 'name',
            type: 'string',
            required: true,
            formShow: !false,
            tableShow: !false,
            minWidth: 180
          },
          {
            label: '型号规格',
            fieldName: 'model',
            type: 'string',
            required: true,
            formShow: !false,
            tableShow: !false,
            minWidth: 180
          },
          {
            label: '数量',
            fieldName: 'nums',
            type: 'number',
            required: true,
            formShow: !false,
            tableShow: !false,
            minWidth: 180
          },
          {
            label: '单位',
            fieldName: 'unit',
            type: 'select',
            optionList: unitList,
            required: true,
            formShow: !false,
            tableShow: !false,
            minWidth: 180
          },
          {
            label: '供应商',
            fieldName: 'supplier',
            type: 'string',
            required: false,
            formShow: !false,
            tableShow: !false,
            minWidth: 180
          },
          {
            label: '层级',
            fieldName: 'level',
            type: 'integer',
            required: false,
            formShow: !false,
            tableShow: !false,
            minWidth: 180
          },
          {
            label: '修改人',
            fieldName: 'updatedBy',
            type: 'string',
            required: false,
            formShow: !true,
            tableShow: !true,
            minWidth: 180
          },
          {
            label: '修改时间',
            fieldName: 'updatedTime',
            type: 'date',
            required: false,
            formShow: !true,
            tableShow: !true,
            minWidth: 180
          },
          {
            label: '创建人',
            fieldName: 'createdBy',
            type: 'string',
            required: false,
            formShow: !true,
            tableShow: !true,
            minWidth: 180
          },
          {
            label: '创建时间',
            fieldName: 'createdTime',
            type: 'date',
            required: false,
            formShow: !true,
            tableShow: !true,
            minWidth: 180
          },
          {
            label: '数据编号',
            fieldName: 'dataNo',
            type: 'string',
            required: false,
            formShow: !true,
            tableShow: !true,
            minWidth: 180
          },
          {
            label: '是否删除 1是 0 否 默认0',
            fieldName: 'isDel',
            type: 'integer',
            required: false,
            formShow: !true,
            tableShow: !true,
            minWidth: 180
          },
          {
            label: '版本 默认1',
            fieldName: 'version',
            type: 'integer',
            required: false,
            formShow: !true,
            tableShow: !true,
            minWidth: 180
          },
          {
            label: '数据状态 ',
            fieldName: 'statusName',
            type: 'string',
            required: false,
            formShow: !true,
            tableShow: !true,
            minWidth: 180
          },
          {
            label: '状态编码',
            fieldName: 'statusCode',
            type: 'string',
            required: false,
            formShow: !true,
            tableShow: !true,
            minWidth: 180
          }
        ]
      },
      rules: {
        idGeneralMaterialsSummary: [
          {
            required: true,
            message: '必填项不能为空',
            trigger: 'blur'
          }
        ],
        name: [
          {
            required: true,
            message: '必填项不能为空',
            trigger: 'blur'
          }
        ],
        bomDesc: [
          {
            required: true,
            message: '必填项不能为空',
            trigger: 'blur'
          }
        ],
        level: [
          {
            required: true,
            message: '必填项不能为空',
            trigger: 'blur'
          }
        ],
        bomNo: [
          {
            required: true,
            message: '必填项不能为空',
            trigger: 'blur'
          }
        ],
        name: [
          {
            required: true,
            message: '必填项不能为空',
            trigger: 'blur'
          }
        ],
        model: [
          {
            required: true,
            message: '必填项不能为空',
            trigger: 'blur'
          }
        ],
        nums: [
          {
            required: true,
            message: '必填项不能为空',
            trigger: 'blur'
          }
        ],
        unit: [
          {
            required: true,
            message: '必填项不能为空',
            trigger: 'blur'
          }
        ],
        supplier: [
          {
            required: true,
            message: '必填项不能为空',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    loadBomDataInfo(row) {
      let _this = this
      let params = [
        {propertyName: 'auxiliaryMaterialNumber', value: row.bomNo}
      ]
      postRequest(_this.materialsOneConditionUrl, params).then((res) => {
        if (res.data.code === 0) {
          let redata = res.data.data
          row.name = redata ? redata.name : ''
          row.model = redata ? redata.model : ''
          row.unit = redata ? redata.unit : ''
          row.nums = redata ? 1 : ''
          row.level = 1
        }
      })
    },
    dataFormat(row, column, cellValue, index) {
      let _this = this;
      return _this.parseCodeToName(column.property, cellValue)
    },
    parseCodeToName(name, cellValue) {
      let _this = this;
      cellValue = _this.commonParseCodeToName(name, cellValue);
      return cellValue;
    },
    /**
     * 初始化数据
     */
    init: function () {
      let _this = this
      _this.dataList = []
      _this.selectData = []
      _this.loadByPage(_this.currentPage, _this.pageSize)
      _this.loadPermissionResources()
      _this.commonSelectData('mat_general_materials_summary')
      //单位
      _this.commonSelectData('dict_unit', function (data) {
        _this.unitList = []
        if (data) {
          data.forEach((item) =>
              _this.unitList.push({
                name: item.statusName,
                value: item.statusCode
              })
          )
        }
      })
    },

    parseSearchData: function () {
      let _this = this
      let data = _this.searchForm
      let arr = []
      //生成的数据，默认所有查询字段完全匹配
      if (data.idGeneralMaterialsSummary) {
        arr.push({
          propertyName: 'idGeneralMaterialsSummary',
          value: data.idGeneralMaterialsSummary,
          operation: 'eq'
        })
      }
      if (data.name) {
        arr.push({
          propertyName: 'name',
          value: data.name,
          operation: 'lk'
        })
      }
      if (data.bomDesc) {
        arr.push({
          propertyName: 'bomDesc',
          value: data.bomDesc,
          operation: 'eq'
        })
      }

      return arr
    }
  },
  created() {
    let _this = this
    _this.init()
  }
}
</script>
<style lang = "scss" scoped></style>
<template>
  <star-horse-dialog
      :visible = "batchDialogTableVisible"
      :title = "dialogTitle"
      is-batch = "true"
      @merge = "merge"
      @mergeDraft = "mergeDraft"
      @reset = "resetForm"
  >
    <star-horse-form-list
        ref = "mergeDataFormBatchRef"
        :field-list = "tableFieldList.fieldList"
        :rules = "rules"
    />
  </star-horse-dialog>
  <star-horse-dialog
      :visible = "dialogTableVisible"
      :title = "dialogTitle"
      @merge = "merge"
      @mergeDraft = "mergeDraft"
      @reset = "resetForm"
  >
    <star-horse-form

        :field-list = "tableFieldList"
        :rules = "rules"
    />
  </star-horse-dialog>
  <star-horse-dialog
      :visible = "viewVisible"
      :title = "'查看数据'"
      :is-view = "true">
    <star-horse-data-view
        :data-format = "parseCodeToName"
        :field-list = "tableFieldList"
        :rule-form = "ruleForm"
    />
  </star-horse-dialog>
  <el-card class = "inner_content">
    <star-horse-search-comp
        @dataSearch = "dataSearch"
        :form-data = "searchFormData"/>
    <el-divider/>
    <star-horse-button-list
        :permission = "permission"
        :upload-url = "uploadUrl"
        @upload = "upload"
        @uploadProcess = "uploadProcess"
        @uploadError = "uploadError"
        @uploadSuccess = "uploadProcess"
        @btnOperation = "btnOperation"
    />
    <star-horse-table-comp
        :primary-key = "primaryKey"
        :field-list = "tableFieldList.fieldList"
        :table-data-id = "tableDataId"
        :data-list = "dataList"
        :table-ref = "'multipleTable'"
        :permission = "permission"
        :data-format-fun = "dataFormat"
        @editData = "editData"
        @handleSelectionChange = "handleSelectionChange"
        @getRowIdentity = "getRowIdentity"
        @editById = "editById"
        @viewById = "viewById"
        @deleteById = "deleteById"
    />

    <el-divider/>
    <el-pagination
        :total = "totalData"
        @current-change = "pageChangeClick"
        @size-change = "pageSizeClick"
        layout = "total, sizes, prev, pager, next, jumper"
        v-model:currentPage = "currentPage"
        v-model:page-size = "pageSize"
        v-model:pageCount = "totalPage"
    />
  </el-card>
</template>
